<?php
include_once F_SOURCE . 'vbc/vbc.model.class.php';
include_once F_SOURCE . 'launch/launch.model.class.php';
include_once F_SOURCE . 'ads/ads.model.class.php';

/**
 * VBC 投放管理
 * 
 * @author Dean
 *
 */
class vbcCtrl extends Ctrl{
	
	public function show(){
		$view = new View();
	
		$pageIndex = ($_GET['pageIndex']=='' || $_GET['pageIndex']<1)?1:$_GET['pageIndex'];
	
		$vbcModel = new vbcModel();
	
		$params = $this->getShowParams();
		$number = $vbcModel->countAll($params);
		$vbcGroupList = $vbcModel->findAll(($pageIndex-1)*PageNum, PageNum, $params);
	
		$page = pageCount($number, $pageIndex, '?do=vbc.show');
	
		$launchModel = new launchModel();
		foreach ($vbcGroupList as $key => $vbcGroup){
			$ad_ids = $vbcGroup->ad_ids;
			$ad_idsArr = explode(',', $ad_ids);
			$importVBCInfo = $launchModel->findLaunchIDByHDID($ad_idsArr[0]);
			$vbcGroupList[$key]->order = $importVBCInfo->order;
			$vbcGroupList[$key]->activity = $importVBCInfo->activity;
			$vbcGroupList[$key]->adsnum = count($ad_idsArr);
			
			if(checkVaildDate($vbcGroup->start, $vbcGroup->end)===true){
				$vbcGroupList[$key]->status = "有效执行";
			}
		}
		
		$data = array(
				'op_select' => 'vbc',
				'child_select' => 'show',
				'launchList' => $vbcGroupList,
				'page' => $page,
				'params'=>$params
		);
	
		$view->display($data, 'vbc-list.html');
	}
	
	public function view(){
		$view = new View();
		$view->display(array('op_select' => 'vbc','child_select' => 'info',), 'vbc-insert.html');
	}
	
	public function info(){
		$view = new View();
	
		$id = $_GET['id'];
	
		$vbcModel = new vbcModel();
		$vbcInfo = $vbcModel->findOne($id);
		
		$adsList = $vbcModel->findVBCAdsByID($vbcInfo->	ad_ids);
		
		$launchModel = new launchModel();
		
		$idList = array();
		foreach ($adsList as $key=>$value) {
			if(isset($value->id)){
				$importLaunchInfo = $launchModel->findImportVBCLaunchByName($value->launch_name);
				if(!isset($importLaunchInfo->id))continue;
				$idList[] = $importLaunchInfo->id;
			}
		}
// 		$ids = implode(',', $idList);
	
		$data = array(
				'op_select' => 'vbc',
				'child_select' => 'info',
				'op' => 'update',
				'obj' => $vbcInfo,
				'adsList' => $adsList,
// 				'adsids' => $ids
		);
	
		$view->display($data, 'vbc-insert.html');
	}
	
	public function modify(){
	
		$id = getPostStringParam('id');
		$launchname =  getPostStringParam('name');
		$start = getPostDateParam('start');
		$end = getPostDateParam('end');
		$weight = getPostIntParam('weight');
		$clickurl = getPostStringParam('clickurl');
		$window = getPostStringParam('window');
		$status = getPostStringParam('status');
		$size = getPostStringParam('size');
		$priority= getPostStringParam('priority');
		$keywords = getPostStringParam('keywords');
		$num = getPostIntParam('num');
		$order = getPostStringParam('order');
		$activity = getPostStringParam('activity');
		$adsnum = getPostIntParam('adsnum');
		
		$articleList = getPostStringParam('articleList');
		if(empty($articleList)){
			$articles = getPostStringParam('articles');
		}else{
			$articles = $this->getUnUsedArticles($num, $articleList);
		}
		
		$time = getNowTime();
		$modified = 1;
	
		$data = array(
				'name' => $launchname,
				'start' => $start,
				'end' => $end,
				'weight' => $weight,
				'click_url' => $clickurl,
				'target_window' => $window,
				'status' => $status,
				'size' => $size,
				'priority' => $priority,
				'keywords' => $keywords,
				'articles' => $articles,
				'modifyTime' => $time,
				'num' => $num,
				'order' => $order,
				'activity' => $activity,
				'adsnum' => $adsnum
		);
	
		$where = array(
				'id' => $id
		);
	
		$vbcModel = new vbcModel();
		$number = $vbcModel->modify($data, $where);
			
		$view = new View();
		
		if($number > 0){
			go('index.php?do=vbc.show&msg=success&id='.$id);
		}else{
			go('index.php?do=vbc.info&msg=error&id='.$id);
		}
	}
	
	public function delete(){
	
		$id = $_GET['id'];
	
		$vbcModel = new vbcModel();
		$vbcModel->delete(array('id'=>$id));
	
		go('index.php?do=vbc.show&msg=success');
	}
	
	private function setLayouts($ids){
		
		if(empty($ids) || $ids == '')return ;
		
		$idArr = explode(',', $ids);
		$adsModel = new adsModel();
		foreach ($idArr as $id) {
			$layout = getPostStringParam('layout_'.$id);
			if(empty($layout))continue;
			
			$data = array('layout'=>$layout);
			$where = array('id'=>$id);
			
			$adsModel->modify($data, $where);
		}
	}
	
	private function getUnUsedArticles($num, $articles){
	
		$usedArticleList = $this->getUsedArticleListFromDB();
	
		$articleList = explode(',', $articles);
		$unUsedArticleList = array_diff($articleList, $usedArticleList);
	
		$lenght = count($unUsedArticleList);
		if($lenght<$num){
			$num = $lenght;
		}
	
		$canUsedArticleList = array_slice($unUsedArticleList, 0, $num);
		return implode(',', $canUsedArticleList);
	}
	
	public function getUsedArticleListFromDB(){
	
		$launchModel = new launchModel();
		$launchList = $launchModel->findAllByType('vbc');
	
		$usedArticle = '';
		foreach ($launchList as $launch) {
			$articles = $launch->articles;
			if(empty($articles) || $articles=='')continue;
			if($usedArticle==''){
				$usedArticle = $articles;
			}else{
				$usedArticle = $usedArticle.','.$articles;
			}
		}
	
		$usedArticleList = explode(',', $usedArticle);
		$usedArticleList = array_unique($usedArticleList);
	
		return $usedArticleList;
	}
	
	public function showimg(){
	
		$id = getPostStringParam('id');
		
		$vbcModel = new vbcModel();
		$vbcInfo = $vbcModel->findOne($id);
		
		$adsModel = new adsModel();
		$adsList = $adsModel->findVBCAdsByLaunchName($vbcInfo->name);
	
		$idList = array();
		foreach ($adsList as $value) {
			$idList[] = $value->id.'@@'.$value->name;
		}
		$ids = implode(',', $idList);
		
		echo $ids;
	}
	
	public function insertGroupByImport($name, $id){
		$nameArr = explode('-', $name);
		if(stripos($name, '-')!==false){
			$vbcModel = new vbcModel();
			$vbcInfo = $vbcModel->findByName($nameArr[0]);
			$time = getNowTime();
				
			if(!isset($vbcInfo->id)){
				$newGroupID = $this->getNextID();
				
				$data = array(
					'id' => $newGroupID,
					'name' => $nameArr[0],
					'ad_ids' => $id,
					'adsnum' => 1,
					'modified' => 0,
					'createTime'	=> $time,
					'modifyTime' => $time,
				);
				
				$vbcModel->add($data);
			}else{
				$newAd_ids = $vbcInfo->ad_ids.','.$id;
				$ad_idsArr = explode(',', $newAd_ids);
				$filterArr = array_unique($ad_idsArr);
				$unique_arr = implode(',', $filterArr);
				
				$newNum = $vbcInfo->adsnum+1;
				
				$data = array(
					'ad_ids' => $unique_arr,
					'adsnum' => $newNum,
					'modifyTime' => $time,
				);
				$where = array('id'=>$vbcInfo->id);
				
				$vbcModel->modify($data, $where);
			}
			return true;
		}else{
			return false;
		}
	}
	
	public function inputOrderintoGroup(){
		
		$vbcModel = new vbcModel();
		$adsModel = new adsModel();
		$vbcList = $vbcModel->findNullOrderGroup();
		if (is_array($vbcList)){
			foreach ($vbcList as $vbcInfo){
				if(!isset($vbcInfo->name))continue;
				$importList = $adsModel->findImportOrderByVBCName($vbcInfo->name);
				foreach ($importList as $importInfo){
					if(!isset($importInfo->order))continue;
					
					$modifyData = array(
						'order' => $importInfo->order,
						'activity' => $importInfo->activity,		
					);
					
					$whereData = array(
						'id' => $vbcInfo->id
					);
					$vbcModel->modify($modifyData, $whereData);
					break;
				}
			}
		}
	}
	
	private function getNextID(){
		$vbcModel = new vbcModel();
		$vbcInfo = $vbcModel->getMaxID();
	
		$nextID = 'HD100001';
	
		if(isset($vbcInfo->id)){
			$maxnum = intval(str_replace('HD', '', $vbcInfo->id));
			$nextID = 'HD'.($maxnum+1);
		}
		return $nextID;
	}
	
	/*
	public function show_l(){
		$view = new View();
	
		$pageIndex = ($_GET['pageIndex']=='' || $_GET['pageIndex']<1)?1:$_GET['pageIndex'];
	
		$launchModel = new launchModel();
	
		$params = $this->getShowParams();
	
		$number = $launchModel->countAllByType("vbc", $params);
		$launchList = $launchModel->findAllByType("vbc", ($pageIndex-1)*PageNum, PageNum, $params);
	
		$page = pageCount($number, $pageIndex, '?do=vbc.show_l');
	
		$data = array(
				'op_select' => 'vbc',
				'child_select' => 'show_l',
				'launchList' => $launchList,
				'page' => $page,
				'params'=>$params
		);
	
		$view->display($data, 'vbc-list-l.html');
	}

	public function infoadd(){
	
		$view = new View();
	
		$id = $_GET['id'];
	
		$launchModel = new launchModel();
		$launchInfo = $launchModel->import_findOne($id);
	
		$adsModel = new adsModel();
		$adsList = $adsModel->findVBCAdsByLaunchName($launchInfo->name);
		
		$idList = array();
		foreach ($adsList as $key=>$value) {
			if($value->layout==""){
				$position = findAdsPositionByUrl($id, $value->id);
				$adsList[$key]->layout=$position;
			}
			$idList[] = $value->id;
		}
		$ids = implode(',', $idList);
		
		$data = array(
				'op_select' => 'vbc',
				'child_select' => 'info',
				'op' => 'add',
				'obj' => $launchInfo,
				'adsList' => $adsList,
				'adsids' => $ids
		);
	
		$view->display($data, 'vbc-insert.html');
	}
	
	// 新增vbc投放操作
	public function add(){
	
		$id = getPostIntParam('id');
		$launchname =  getPostStringParam('name');
		$start = getPostDateParam('start');
		$end = getPostDateParam('end');
		$weight = getPostIntParam('weight');
		$clickurl = getPostStringParam('clickurl');
		$window = getPostStringParam('window');
		$status = getPostStringParam('status');
		$size = getPostStringParam('size');
		$priority= getPostStringParam('priority');
		$keywords = getPostStringParam('keywords');
		$num = getPostIntParam('num');
		$order = getPostStringParam('order');
		$activity = getPostStringParam('activity');
		$adsnum = getPostIntParam('adsnum');
		$type = 'vbc';
		
		$articleList = getPostStringParam('articleList');
		$articles = $this->getUnUsedArticles($num, $articleList);
		
		// set VBC's ads layout
		$ids = getPostStringParam('adsids');
		$this->setLayouts($ids);
	
		$time = getNowTime();
		$createUser = $_SESSION['ads']['user']->id;
	
		$data = array(
				'name' => $launchname,
				'start' => $start,
				'end' => $end,
				'weight' => $weight,
				'click_url' => $clickurl,
				'target_window' => $window,
				'status' => $status,
				'size' => $size,
				'priority' => $priority,
				'keywords' => $keywords,
				'createTime' => $time,
				'modifyTime' => $time,
				'createUser' => $createUser,
				'articles' => $articles,
				'num' => $num,
				'type' => $type,
				'id' => $id,
				'order' => $order,
				'activity' => $activity,
				'adsnum' => $adsnum
		);
	
		$launchModel = new launchModel();
		$insertID = $launchModel->add($data);
			
		if($insertID > 0){
			$launchModel->import_modify(array('op'=>'c'), array('id'=>$id));
				
			go('index.php?do=vbc.show&msg=success&id='.$id);
		}else{
			go('index.php?do=vbc.info&msg=error&id='.$id);
		}
	}
	
	*/
	
	private function getShowParams(){
		$data = array();
	
		$data['v_id'] = getPostStringParam('v_id');
		$data['v_order'] = getPostStringParam('v_order');
		$data['v_activity'] = getPostStringParam('v_activity');
		$data['v_name'] = getPostStringParam('v_name');
		$data['v_adsnum'] = getPostStringParam('v_adsnum');
		$data['v_start'] = getPostStringParam('v_start');
		$data['v_end'] = getPostStringParam('v_end');
		$data['v_priority'] = getPostStringParam('v_priority');
		$data['v_keywords'] = getPostStringParam('v_keywords');
		$data['v_num'] = getPostStringParam('v_num');
	
		return $data;
	}
	
}